home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ccdl150l.zip / IO / FOPEN.C < prev    next >
C/C++ Source or Header  |  1997-04-11  |  2KB  |  110 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <libp.h>
  6.  
  7. extern FILE *_pstreams[_NFILE_];
  8. extern char *_filenames[_NFILE_];
  9. extern int maxfiles;
  10.  
  11. FILE *_basefopen(const char *name, const char *mode,FILE *stream)
  12. {
  13.     int flags = 0,append = 0, update = 0, id = 0,i;
  14.     FILE *file;
  15.     char *buf,*fname;
  16.     if (maxfiles == _NFILE_)
  17.         return 0;
  18.     for (i=0; i < strlen(mode); i++) {
  19.         switch (mode[i]) {
  20.             case 'r':
  21.                 flags |= _F_READ;
  22.                 break;
  23.             case 'w':
  24.                 flags |= _F_WRIT;
  25.                 break;
  26.             case 'a':
  27.                 flags |= _F_WRIT;
  28.                 append = 1;
  29.                 break;
  30.             case '+':
  31.                 update = 1;
  32.                 break;
  33.             case 'b':
  34.                 flags |= _F_BIN;
  35.                 break;
  36.             case 't':
  37.                 flags &= ~_F_BIN;
  38.                 break;
  39.             default:
  40.                 return 0;
  41.         }
  42.     }
  43.     if (!(flags & (_F_READ | _F_WRIT)))
  44.         return 0;
  45.     fname = malloc(strlen(name)+1);
  46.     if (!fname) 
  47.         return 0;
  48.     strcpy(fname,name);
  49.     if (stream)
  50.         file = stream;
  51.     else
  52.         if ((file = malloc(sizeof(FILE))) == 0) {
  53.             free(fname);
  54.             return 0;
  55.         }
  56.         else
  57.             memset(file,0,sizeof(FILE));
  58.     file->flags = 0;
  59.     buf = malloc(BUFSIZ);
  60.     if (!buf) {
  61.         free(fname);
  62.         free(file);
  63.         return 0;
  64.     }
  65.     switch (flags & (_F_READ | _F_WRIT)) {
  66.         case 0:
  67.             goto nofile;
  68.         case _F_READ:
  69.             if (update)
  70.                 flags |= _F_WRIT;
  71.              id = _ll_open(name,    _ll_flags(flags));
  72.             break;
  73.         case _F_WRIT:
  74.             if (update)
  75.                 flags |= _F_READ;
  76.             id = _ll_creat(name,_ll_flags(flags));
  77.             break;
  78.         case _F_READ | _F_WRIT:
  79.             return 0;
  80.     }
  81.     if (id == 0)
  82.         goto nofile;
  83.     file->token = FILTOK;
  84.     file->level = 0;
  85.     file->fd = (char) id;
  86.     file->flags |= flags;
  87.     file->istemp = 0;
  88.     file->hold = 0;
  89.     if (flags & _F_BIN)
  90.         setvbuf(file,buf,_IOFBF,BUFSIZ);
  91.     else
  92.         setvbuf(file,buf,_IOLBF,BUFSIZ);
  93.     flags |= _F_BUF;
  94.     if (append) {
  95.         if (fseek(file,0,SEEK_END)) {
  96. nofile:
  97.             free(fname);
  98.             free(file->buffer);
  99.             free(file);
  100.             return 0;
  101.         }
  102.     }
  103.     _filenames[maxfiles] = fname;
  104.     _pstreams[maxfiles++] = file;
  105.     return file;
  106. }
  107. FILE *fopen(const char *name, const char *mode)
  108. {
  109.     return _basefopen(name,mode,0);
  110. }